<template>
	<view class="order-detail-wrap">
		<view class="header bg-white">
			<view class="title flex-x-between">
				<view class="name bold">
					<view class="msg-icon inline-block text-center">
						<text :class="[`iconfontPT ${judgeIcon(orderDetailData.type)}`]"></text>
					</view>
					{{orderDetailData.title}}
				</view>
				<text class="distance">{{orderDetailData.distance ? orderDetailData.distance : ''}}</text>
			</view>
			<view v-if="orderDetailData.type==1">
				<view class="case flex-x-start" v-for="(item, index) in orderDetailData.goods" :key="index">
					<view class="left">
						<image v-if="item.img" :src="`${host}/${item.img}`" class="avatar"></image>
					</view>
					<view class="info flex-x-between relative">
						<view class="center">
							<text class="name block">{{item.title}}</text>
							<text class="amount theme block">￥{{item.price}}</text>
						</view>
						<view class="right">
							X{{item.num}}
						</view>
					</view>
				</view>
			</view>
			<view v-else>
				<view class="case flex-x-start">
					<view class="left">
						<image  :src="`${qiniuhost}${orderDetailData.img}`" class="avatar" mode="aspectFill" v-if="orderDetailData.img && orderDetailData.type==3"></image>
						<image  v-if="orderDetailData.img && orderDetailData.type!=3" :src="`${host}${orderDetailData.img}`" class="avatar" mode="aspectFill" ></image>
					</view>
					<view class="info flex-x-between relative">
						<view class="center">
							<text class="name block">{{orderDetailData.order_title}}</text>
							<text class="amount theme block">￥{{orderDetailData.pay_money}}</text>
						</view>
						<view class="right" v-if="orderDetailData.number">
							X{{orderDetailData.number}}
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="order-info-wrap bg-white" v-if="!refund">
			<view class="title flex-x-start bold">
				订单信息
			</view>
			<view class="order-info-item flex-x-between">
				<text class="label">订单号</text>
				<text class="value">{{orderDetailData.order_no}}</text>
			</view>
			<view class="order-info-item flex-x-between">
				<text class="label">下单时间</text>
				<text class="value">{{orderDetailData.order_time}}</text>
			</view>
			<view class="order-info-item flex-x-between" v-if="orderDetailData.type==1">
				<text class="label">总价</text>
				<text class="value">￥{{orderDetailData.total_money}}</text>
			</view>
			<view class="order-info-item flex-x-between" v-if="orderDetailData.type==1">
				<text class="label">优惠</text>
				<text class="value theme">-￥{{orderDetailData.district_money}}</text>
			</view>
			<view class="order-info-item flex-x-between" v-if="orderDetailData.type==1">
				<text class="label">实付</text>
				<text class="value theme">￥{{orderDetailData.pay_money}}</text>
			</view>
			<view class="order-info-item flex-x-between" v-if="orderDetailData.type!==1">
				<text class="label">实付金额</text>
				<text class="value theme">￥{{orderDetailData.pay_money}}</text>
			</view>
			<view class="bottom flex-x-end">
				{{orderDetailData.type==1 ? '实付金额' : '合计'}}：<text class="pay-amount theme bold">￥{{orderDetailData.pay_money}}</text>
			</view>
		</view>
		<view class="QR-code bg-white" v-if="refund">
			<view class="title flex-x-start bold">
				退款金额：<text class="theme">￥{{orderDetailData.pay_money}}</text>
			</view>
			<view class="title flex-x-start bold">
				退款说明：
				<textarea style="margin-top: 65rpx;" placeholder="选填" class="w100" auto-height :fixed="true" maxlength="300"
				 cursor-spacing="20" :show-confirm-bar="false" v-model="form.content" confirm-type="send"></textarea>
			</view>
		</view>
		<view class="QR-code bg-white text-center" v-if="orderDetailData.type==1&&!refund">
			<view class="title flex-x-start bold">
				核销二维码
			</view>
			<image :src="`${codeHost}/${orderDetailData.code_url}`" mode=""></image>
		</view>
		<view class="btn-wrap bg-white w100 flex-x-end" v-if="(orderDetailData.state==0||(orderDetailData.state==1&&orderDetailData.type==1&&!refund)||refund)">
			<view class="btn cancel-btn text-center" v-if="orderDetailData.state==0" @click="handleCancelOrder(orderDetailData.order_no)">
				取消订单
			</view>
			<view class="btn pay-btn text-center" v-if="orderDetailData.state==0" @click="handlePayment(orderDetailData)">
				立即付款
			</view>
			<view class="btn refund-btn text-center" v-if="orderDetailData.state==1&&orderDetailData.type==1&&!refund" @click="handleShowRefund">
				申请退款
			</view>
			<view class="btn refund-confirm-btn text-center" v-if="refund" @click="handleRefund(orderDetailData.order_no)">
				确认退款
			</view>
		</view>
	</view>
</template>

<script>
	import {
		refund,
		payment,
		cancelMyOrder,
		getMyOrderDetail
	} from '../../../apis/personal/index.js'

	export default {
		data() {
			return {
				refund: false,
				orderId: 0,
				payAmount: 0,
				orderDetailData: {}
			}
		},
		onLoad(option) {
			this.refund = false
			this.orderId = option.id
			this.getOrderDetailList(this.orderId)
		},
		computed: {
			judgeIcon(type) {
				let icon = ''
				return function(type) {
					switch (type) {
						case 1:
							icon = 'iconmendian'
							break;
						case 2:
							icon = 'iconcheliang'
							break;
						case 3:
							icon = 'iconcheliang'
							break;
						case 4:
							icon = 'iconzhiwei'
							break;
						case 5:
							icon = 'iconhuodong'
							break;
						case 6:
							icon = 'iconhuodong'
							break;
					}
					return icon
				}
			},
			host() {
				return this.$baseHost.car
			},
			qiniuhost() {
				return this.$baseHost.qiniuHost
			},
			codeHost() {
				return this.$baseHost.lifeCircleHost
			},
		},
		methods: {
			handleShowRefund() {
				this.refund = true
			},
			handleRefund(order_no) {
				this.$loading.show()
				refund({
					order_no
				}).then(res => {
					this.$loading.hide()
					uni.showToast({
						title: '退款成功！',
						icon: 'success'
					})
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						})
					}, 1000)
				})
			},
			handleCancelOrder(order_no) {
				let _this = this
				uni.showModal({
					title: "提示",
					content: "确认删除该订单？",
					success: res => {
						if (res.confirm) {
							_this.$loading.show()
							cancelMyOrder({
								order_no
							}).then(_res => {
								if (+_res.code === 200) {

									uni.showToast({
										title: _res.info,
										icon: 'success'
									})
									_this.$loading.hide();
									uni.navigateBack({
										delta: 1
									})
								}
							})
						}
					}
				});
			},
			handlePayment(item) {
				let _this = this,
					params = {
						total_money: item.total_money,
						district_money: item.district_money,
						pay_money: item.pay_money,
						coupon_id: item.coupon_id ? item.coupon_id : '',
						item_id: item.type,
						store_id: item.store_id,
						factory_id: item.type,
						type: item.type,
						order_title: item.order_title,
						num: item.type,
						goods_id: item.type,
						img: item.type,
						order_no: item.order_no
					};

				payment(params).then(res => {

					if (res.code == 1) {

						uni.requestPayment({
							appId: res.data.appId,
							nonceStr: res.data.nonceStr,
							package: res.data.package,
							paySign: res.data.paySign,
							signType: res.data.signType,
							timeStamp: res.data.timeStamp,
							success(res) {
								uni.showToast({
									title: '支付成功！',
									icon: 'success'
								})
								_this.getOrderDetailList(_this.orderId)
							},
							fail(res) {}
						})
					}
				})
			},
			getOrderDetailList(order_no) {
				this.$loading.show()

				getMyOrderDetail({
					order_no
				}).then(res => {
					this.$loading.hide()
					this.orderDetailData = res.data
				})
			}
		}
	}
</script>

<style lang="less">
	.order-detail-wrap {
		background: #f8f8f8;
		padding-top: 30rpx;

		.msg-icon {
			width: 36rpx;
			height: 36rpx;
			background: #E41805;
			border-radius: 100%;
			text-align: center;
			line-height: 30rpx;
			margin-right: 15rpx;
			vertical-align: 5rpx;

			.iconfontPT {
				color: #ffffff;
				font-size: 20rpx;
			}
		}

		.title {
			padding: 20rpx 0;
			border-bottom: 1rpx solid rgba(203, 203, 203, .5);

			.name {
				height: 40rpx;
				line-height: 40rpx;
			}
		}

		.header {
			padding: 0 30rpx;
			margin-bottom: 30rpx;

			.title {
				.distance {
					font-size: 24rpx;
					color: #999999;
				}
			}

			.case {
				padding: 30rpx 0;

				&:last-child {
					.info {
						width: 550rpx;
						font-size: 28rpx;

						&::after {
							display: none;
						}
					}
				}

				.left {
					margin-right: 20rpx;
				}

				.info {
					width: 550rpx;
					font-size: 28rpx;

					&::after {
						position: absolute;
						display: inline-block;
						content: '';
						width: 100%;
						height: 2rpx;
						background: rgba(203, 203, 203, .5);
						bottom: -50rpx;
						left: 0;
					}

					.center {

						.amount {
							font-size: 20rpx;
						}
					}

					.right {
						font-size: 20rpx;
						color: #CBCBCB;
					}
				}

				.avatar {
					width: 120rpx;
					height: 120rpx;
					border-radius: 10rpx;
				}
			}
		}

		.order-info-wrap {
			padding: 0 30rpx;
			margin-bottom: 30rpx;

			.order-info-item {
				margin: 20rpx 0;
				font-size: 28rpx;

				.label {
					color: #999999;
				}
			}

			.bottom {
				padding: 20rpx 0;
				border-top: 1rpx solid rgba(203, 203, 203, .5);
			}
		}

		.QR-code {
			padding: 0 30rpx;
			margin-bottom: 250rpx;

			image {
				height: 400rpx;
				width: 400rpx;
				margin-top: 40rpx;
			}
		}

		.btn-wrap {
			height: 160rpx;
			position: fixed;
			left: 0;
			bottom: 0;
			z-index: 111;
			padding-top: 15rpx;
			box-shadow: -1rpx -2rpx 7rpx 0rpx rgba(0, 0, 0, 0.1);

			.btn {
				width: 172rpx;
				height: 58rpx;
				font-size: 26rpx;
				border-radius: 27rpx;
				line-height: 58rpx;
				margin: 0 20rpx;
				border: 2rpx solid rgba(203, 203, 203, 1);
			}
		}
	}
</style>
